package com.wang.controller;

import com.wang.Param.UserLoginPhoneParam;
import com.wang.Param.UserLoginUserAccountParam;
import com.wang.commons.exception.ParamException;
import com.wang.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Slf4j
@Api(tags = "用户登录")
public class LoginController {

    @ApiOperation("使用账号或邮箱登录接口")
    @PostMapping("/userAccountOrEmailLogin")
    public Result toUseAccountLogin(@ApiParam(name = "userLoginUserAccountParam", value = "登录参数", required = true) @Valid @RequestBody UserLoginUserAccountParam userLoginUserAccountParam, BindingResult bindingResult){
        log.info("进行参数校验：{}", userLoginUserAccountParam);
        if(bindingResult.hasErrors()){
            throw new ParamException();
        }
        log.info("userLoginUserAccountParam:{}", userLoginUserAccountParam);
        if(ObjectUtils.isEmpty(userLoginUserAccountParam)){
            log.info("登录参数不为空:{}", userLoginUserAccountParam);
            if(userLoginUserAccountParam.getUserAccount().endsWith("com")&&userLoginUserAccountParam.getUserAccount().contains("@")){
                log.info("执行邮箱登录:{}", userLoginUserAccountParam);
                return null;
            }else{
                log.info("执行账号登录:{}", userLoginUserAccountParam);
                return null;
            }
        }
        log.info("执行登录失败");
        return null;
    }


    @ApiOperation("使用手机号登录接口")
    @PostMapping("/userPhoneLogin")
    public Result userPhoneLogin(@ApiParam(name = "userLoginPhoneParam", value = "登录参数", required = true) @Valid @RequestBody UserLoginPhoneParam userLoginPhoneParam,BindingResult bindingResult){
        log.info("进行参数校验：{}", userLoginPhoneParam);
        if(bindingResult.hasErrors()){
            throw new ParamException();
        }
        log.info("userLoginPhoneParam:{}", userLoginPhoneParam);
        if(ObjectUtils.isEmpty(userLoginPhoneParam)){
            log.info("手机登录参数不为空:{}", userLoginPhoneParam);
            log.info("执行手机登录{}", userLoginPhoneParam);
            return null;
        }
        log.info("执行登录失败");
        return null;
    }
}
